package community.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.annotation.Log;
import community.hospital.annotation.Module;
import community.hospital.annotation.NeedLogin;
import community.hospital.annotation.NeedPermission;
import community.hospital.common.Add;
import community.hospital.common.BaseRemove;
import community.hospital.common.R;
import community.hospital.common.Update;
import community.hospital.entity.request.ListRoleRequest;
import community.hospital.entity.request.RoleRequest;
import community.hospital.entity.response.RoleResponse;
import community.hospital.service.application.RoleApplicationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static community.hospital.common.Constant.*;
import static community.hospital.common.PermissionConstant.*;

/**
 * @Author：ydj
 * @Date：2023/1/26 14:29
 */
@RestController
@RequestMapping("/role")
@Module("角色")
public class RoleController {
    @Resource
    private RoleApplicationService roleApplicationService;

    @PostMapping("/listRole")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = LIST_ROLE)
    public R<Page<RoleResponse>> listRole(@Valid @RequestBody ListRoleRequest request) {
        Page<RoleResponse> responsePage = roleApplicationService.listRole(request);
        return R.ok(responsePage);
    }

    @PostMapping("/addRole")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = ADD_ROLE)
    @Log(methodName = LOG_INSERT,desc = "新增角色")
    public R<Boolean> addRole(@Validated(Add.class) @RequestBody RoleRequest request) {
        Boolean isOk = roleApplicationService.addRole(request);
        return R.ok(isOk);
    }
    @Log(methodName = LOG_REMOVE,desc = "删除角色")
    @PostMapping("/removeRole")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = REMOVE_ROLE)
    public R<Boolean> removeRole(@Valid @RequestBody BaseRemove request) {
        Boolean isOk = roleApplicationService.removeRole(request);
        return R.ok(isOk);
    }

    @PostMapping("/updateRole")
    @NeedLogin
    @Log(methodName = LOG_UPDATE,desc = "更新角色")
    @NeedPermission(role = SUPER_ADMIN, permission = UPDATE_ROLE)
    public R<Boolean> updateRole(@Validated(Update.class) @RequestBody RoleRequest request) {
        Boolean isOk = roleApplicationService.updateRole(request);
        return R.ok(isOk);
    }
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN,permission = LIST_ROLE)
    @Log(methodName = LOG_UPDATE,desc = "查询所有角色")
    @GetMapping("/getAllRoleList")
    public R<List<RoleResponse>> getAllRole() {
        List<RoleResponse> listRoleResp = roleApplicationService.listAllRole();
        return R.ok(listRoleResp);
    }
}
